<?php
class item_pack extends template_item
{
	var $default_name = "Item Pack";
	var $default_description = "Store multiple inventory items in this. Storing: {stored_list}";

	var $allow_multiple = 1;
	
	var $item_config = array();
	var $description = "";
	
	function load_item(&$item)
	{
		parent::load_item($item);
		
		if($item['item_config'] || $item['item_id'] != 0)
		{
			$this->item_config = unserialize($item['item_config']);
		}
		else
		{
			$this->item_config['items'] = array();
			$this->item_config['multiple_format'] = '{number}x {item}';
			$this->item_config['none'] = 'Nothing';
		}
	}
	
	function admin_form()
	{
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"Tags for item name and description",
															"{stored_list} = The list of items using the [list] bbcode<br />{stored_line} = The list of items using one line like a, b, c",
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row( array("<b>Items to store</b><br />By not storing any item, you give people the option to store their own invetory items in this item.",
															  $this->ipsclass->market_admin->compile_item_list("item[".$this->item['item_id']."][items]",$this->item_config['items'],'item_table_'.$this->item['item_id'],'item_id_'.$this->item['item_id'],'quan_id_'.$this->item['item_id'])
													 )      );
		
		$html .= $this->ipsclass->adskin->add_td_row( array("<b>Multiple Item list format</b><br />{item} = Item Name<br />{number} = Number of items for that item",
															  $this->ipsclass->adskin->form_input("item[".$this->item['item_id']."][multiple_format]",$this->item_config['multiple_format'])
													 )      );
		
		$html .= $this->ipsclass->adskin->add_td_row( array("<b>No Item list</b>",
															  $this->ipsclass->adskin->form_input("item[".$this->item['item_id']."][none]",$this->item_config['none'])
													 )      );
		
		return $html;
	}
	
	function admin_save(&$item_info, $input)
	{
		$save = array();
		$save['items'] = $this->ipsclass->market_admin->parse_input_item_list(array('item',$this->item['item_id'],'items'));
		$save['multiple_format'] = $input['multiple_format'];
		$save['none'] = $input['none'];
		
		if($save['items'][$this->item['item_id']] > 0)
		{
			$this->ipsclass->admin->error('You cannot include the item itself into the list');
		}
		
		$item_info['item_config'] = serialize($save);
	}
	
	function public_use_form($inventory_ids = array())
	{
		$this->ipsclass->load_language('lang_market_items');
		
		$hidden_fields = $this->_get_hidden_fields($inventory_ids);
		
		$inv_id = reset($inventory_ids);
		$setting = unserialize($this->ipsclass->market_class->inventory_id[$inv_id]['settings']);
		
		if($this->ipsclass->market_class->inventory_id[$inv_id]['settings'] && $setting)
		{
			return "";
		}
		
		$inventory = "";
		foreach($this->ipsclass->market_class->inventory as $item_group)
		{
			foreach($item_group as $item)
			{
				if($item['i_id'] == $inv_id)
				{
					continue;
				}
				$item['expired'] = $item['expired'] == 0 ? $this->ipsclass->lang['never'] : $this->ipsclass->get_date($item['expired'],'TINY');
				$item['time_bought'] = $this->ipsclass->get_date($item['time_bought'],'TINY');
				$item['fprice_bought'] = $item['price_bought'] <= 0 ? $this->ipsclass->lang['free'] : $this->ipsclass->numbers->format_currency($item['price_bought']);
				$item['fresell_price'] = $item['resell_price'] <= 0 ? $this->ipsclass->lang['trash'] : $this->ipsclass->numbers->format_currency($item['resell_price']);
				$item['description'] = $this->ipsclass->parser->pre_display_parse($this->ipsclass->parser->pre_db_parse($this->ipsclass->market_class->items_loaded[$item['item']]->parse_single_description($item)));
				$inventory .= "		<tr>
			<td class='row1' align='center' valign='middle'>
				<img src='{$this->ipsclass->market_class->image_url_path}/icons/{$this->ipsclass->cache['market_items'][$item['item']]['icon']}' border='0' alt='{$this->ipsclass->lang['item_icon']}' />
			</td>
			<td class='row2' align='left' valign='middle'>
				<b>{$this->ipsclass->cache['market_items'][$item['item']]['item_name']}</b>
				<br />
				{$item['description']}
			</td>
			<td class='row1' align='left' valign='middle'>
				{$this->ipsclass->lang['category']}: {$this->ipsclass->cache['market_category'][$this->ipsclass->cache['market_items'][$item['item']]['category']]['cat_name']}
				<br />
				{$this->ipsclass->lang['time']}: {$item['time_bought']}
				<br />
				{$this->ipsclass->lang['expires']}: {$item['expired']}
			</td>
			<td class='row2' align='left' valign='middle'>
				{$this->ipsclass->lang['resell']}: {$item['fresell_price']}
				<br />
				{$this->ipsclass->lang['price']}: {$item['fprice_bought']}
			</td>
			<td class='row1' align='center' valign='middle'>
				<input type='checkbox' name='inv_id[{$item['i_id']}]' value='1' />
			</td>
		</tr>\n";
			}
		}
		
		return <<<EOF
<div class='formsubtitle'>{$this->ipsclass->lang['pick_items_to_add']}</div>
<form action='{$this->base_url}' method='post'>
{$hidden_fields}
	<table class='ipbtable' cellspacing='0' cellpadding='0'>
		<tr>
			<td align='center' class='subtitle' colspan='5'>
				{$this->ipsclass->lang['ustore_add_store_items']}
			</td>
		</tr>
		<tr>
			<td class='darkrow1' align='center' width='1%'>
				<b>{$this->ipsclass->lang['icon']}</b>
			</td>
			<td class='darkrow3' align='center' width='50%'>
				<b>{$this->ipsclass->lang['name']}/{$this->ipsclass->lang['description']}</b>
			</td>
			<td class='darkrow1' align='center'>
				<b>{$this->ipsclass->lang['category']}</b>/<b>{$this->ipsclass->lang['time_bought']}</b>/<b>{$this->ipsclass->lang['expires']}</b>
			</td>
			<td class='darkrow3' align='center'>
				<b>{$this->ipsclass->lang['resell']}/{$this->ipsclass->lang['price']}</b>
			</td>
			<td class='darkrow1' align='center' width='1%'>
				<b>{$this->ipsclass->lang['pack_item']}</b>
			</td>
		</tr>
{$inventory}
		<tr>
			<td class='darkrow1' align='center' colspan='5'>
				<input type='submit' name='item_used' value='{$this->ipsclass->lang['pack_items']}' class='forminput' />
			</td>
		</tr>
	</table>
</form>
EOF;
	}
	
	function input_done($inventory_ids = array())
	{
		$inv_id = reset($inventory_ids);
		$setting = unserialize($this->ipsclass->market_class->inventory_id[$inv_id]['settings']);
		
		return ((!$this->ipsclass->market_class->inventory_id[$inv_id]['settings'] || !$setting) && isset($this->ipsclass->input['item_used']) && isset($this->ipsclass->input['inv_id']) && is_array($this->ipsclass->input['inv_id']) && count($this->ipsclass->input['inv_id']));
	}
	
	function public_use_item($inventory_ids = array()) 
	{
		$this->ipsclass->load_language('lang_market_items');
		
		$inv_id = reset($inventory_ids);
		$setting = unserialize($this->ipsclass->market_class->inventory_id[$inv_id]['settings']);
		
		if($this->ipsclass->market_class->inventory_id[$inv_id]['settings'] && $setting)
		{
			// Unpack items
			foreach($setting as $item)
			{
				$this->ipsclass->DB->do_insert('market_inventory',$item);
			}
			
			$this->delete_items($inventory_ids);
			$this->_write_log($inv_id, "Unpacked items");
			
			$this->ipsclass->market_class->redirect('items_unpacked');
		}
		else
		{
			// Pack items
			$items = array();
			foreach(array_keys($this->ipsclass->input['inv_id']) as $i_id)
			{
				if($i_id == $inv_id || !isset($this->ipsclass->market_class->inventory_id[$i_id]))
				{
					continue;
				}
				$items[$i_id] = $this->ipsclass->market_class->inventory_id[$i_id];
			}
			if(!$items)
			{
				$this->ipsclass->Error(array('MSG'=>'no_items_packing'));
			}
			$setting = serialize(array_values($items));
			
			$this->ipsclass->DB->do_delete('market_inventory',"i_id IN (".implode(",",array_keys($items)).")");
			$this->ipsclass->DB->do_update('market_inventory',array(
																		'settings' => $setting,
											),"i_id=".$inv_id);
			
			$this->_write_log($inv_id, "Packed items");
			
			$this->ipsclass->market_class->redirect('items_packed');
		}
	}
	
	function parse_store_description()
	{
		$items = array();
		foreach($this->item_config['items'] as $item_id => $quan)
		{
			if(!isset($this->ipsclass->cache['market_items'][$item_id]))
			{
				continue;
			}
			if($quan == 1)
			{
				$items[] = $this->ipsclass->cache['market_items'][$item_id]['item_name'];
			}
			else
			{
				$items[] = str_replace(array('{number}','{item}'),array($this->ipsclass->numbers->do_number_format($quan),$this->ipsclass->cache['market_items'][$item_id]['item_name']),$this->item_config['multiple_format']);
			}
		}
		if(count($items) == 0)
		{
			$items = array($this->item_config['none']);
		}
		
		return str_replace(array('{stored_list}','{stored_line}'),array("[list][*]".implode("[*]",$items)."[/list]",implode(", ",$items)),$this->item['item_description']);
	}
	
	function parse_group_description($inv_items=array())
	{
		$items = array();
		foreach($inv_items as $inv_item)
		{
			if($inv_item['settings'])
			{
				$item_inv = unserialize($inv_item['settings']);
				foreach($item_inv as $item)
				{
					$items[$item['item']]++;
				}
			}
			else
			{
				$items[0] = 0;
			}
		}
		if(count($items) == 0)
		{
			$items = array(0);
		}
		foreach($items as $item_id => $quan)
		{
			if($item_id == 0)
			{
				$items[$item_id] = $this->item_config['none'];
			}
			else
			{
				if(!isset($this->ipsclass->cache['market_items'][$item_id]))
				{
					continue;
				}
				if($quan == 1)
				{
					$items[$item_id] = $this->ipsclass->cache['market_items'][$item_id]['item_name'];
				}
				else
				{
					$items[$item_id] = str_replace(array('{number}','{item}'),array($this->ipsclass->numbers->do_number_format($quan),$this->ipsclass->cache['market_items'][$item_id]['item_name']),$this->item_config['multiple_format']);
				}
			}
		}
		
		return str_replace(array('{stored_list}','{stored_line}'),array("[list][*]".implode("[*]",$items)."[/list]",implode(", ",$items)),$this->item['item_description']);
	}
	
	function parse_single_description($inv_item=array())
	{
		$items = array();
		if($inv_item['settings'])
		{
			$item_inv = unserialize($inv_item['settings']);
			foreach($item_inv as $item)
			{
				$items[$item['item']]++;
			}
		}
		if(count($items) == 0)
		{
			$items = array($this->item_config['none']);
		}
		else
		{
			foreach($items as $item_id => $quan)
			{
				if(!isset($this->ipsclass->cache['market_items'][$item_id]))
				{
					continue;
				}
				if($quan == 1)
				{
					$items[$item_id] = $this->ipsclass->cache['market_items'][$item_id]['item_name'];
				}
				else
				{
					$items[$item_id] = str_replace(array('{number}','{item}'),array($this->ipsclass->numbers->do_number_format($quan),$this->ipsclass->cache['market_items'][$item_id]['item_name']),$this->item_config['multiple_format']);
				}
			}
		}
		
		return str_replace(array('{stored_list}','{stored_line}'),array("[list][*]".implode("[*]",$items)."[/list]",implode(", ",$items)),$this->item['item_description']);
	}
	
	function public_on_create_item(&$inv_item)
	{
		if($this->item_config['items'])
		{
			$items = array();
			$time = time();
			foreach($this->item_config['items'] as $item_id => $quanity)
			{
				if(!isset($this->ipsclass->cache['market_items'][$item_id]))
				{
					continue;
				}
				$expired = $this->ipsclass->cache['market_items'][$item_id]['expire_time'] > 0 ? $time + $this->ipsclass->cache['market_items'][$item_id]['expire_time'] * $this->ipsclass->cache['market_items'][$item_id]['expire_unit'] : 0;
				for($i = 0; $i < $quanity; $i++)
				{
					$inv = array(
									'member_id' => $this->ipsclass->member['id'],
									'item' => $item_id,
									'price_bought' => $this->item['price'],
									'resell_price' => 0,
									'time_bought' => $time,
									'settings' => '',
									'expired' => $expired,
								);
					if($this->ipsclass->market_class->items_loaded[$item_id]->public_on_create_item($inv_item))
					{
						$items[] = $inv;
					}
				}
			}
			$inv_item['settings'] = serialize($items);
		}
		return true;
	}
}
?>